package com.example.money.Controller.Excel;

import com.example.money.Entity.table;
import com.example.money.utls.ExcelUtls;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.List;
@Controller
public class Excelinventory {
    /**
     * 出库信息库存表
     */
    @Resource
    public ExcelUtls excel;

    @GetMapping(value = "/Einventory")
    public void downloadAllClassmate(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();//创建HSSFWorkbook对象,  excel的文档对象
        HSSFSheet sheet = workbook.createSheet("提货信息分表"); //excel的表单

        List<table> classmateList = excel.downloadall();

        String fileName = "inventory" + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {
                "产品ID",
                "产品名称",
                "购买者",
                "购买数量",
                "操作员工",
                "库存总数",
                "剩余库存",
                "提货数量",
                "提货人",
                "备注",
                "操作时间",
                "更新时间"
        };
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (table product : classmateList) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(product.getProduct_id());
            row1.createCell(1).setCellValue(product.getProduct());
            row1.createCell(2).setCellValue(product.getName());
            row1.createCell(3).setCellValue(product.getQuantity());
            row1.createCell(4).setCellValue(product.getStaff());
            row1.createCell(5).setCellValue(product.getInventory());
            row1.createCell(6).setCellValue(product.getSy_inventory());
            row1.createCell(7).setCellValue(product.getPick());
            row1.createCell(8).setCellValue(product.getConsignee());
            row1.createCell(9).setCellValue(product.getRemark());
            row1.createCell(10).setCellValue(product.getCreate_time());
            row1.createCell(11).setCellValue(product.getUp_data());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        workbook.write(response.getOutputStream());
    }
}
